package com.example.jl.bethinner.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.jl.bethinner.domain.BFNewModel;
import com.example.jl.bethinner.domain.City;
import com.google.gson.reflect.TypeToken;
import com.joanzapata.android.BaseAdapterHelper;
import com.joanzapata.android.QuickAdapter;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import java.util.List;

/**
 * Created by JL on 4/3/15.
 */
public class CityChooserDialog extends DialogFragment implements AdapterView.OnItemClickListener {

    static String ARG_DATAS = "ARG_DATAS";
    private String provinceid;
    Context context;
    List<City> cities;
    ListView listView = null;
    OnCitySelectedLisenter lisenter;
    QuickAdapter<City> adapter = null;


    public static CityChooserDialog newInstance(String provinceid) {
        CityChooserDialog fragment = new CityChooserDialog();
        Bundle args = new Bundle();
        args.putSerializable(ARG_DATAS, provinceid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            provinceid = getArguments().getString(ARG_DATAS);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        listView = new ListView(context);
        setCity();
        listView.setOnItemClickListener(this);
        getDialog().setTitle("选择城市");
        return listView;
    }

    public void setCity() {

        Ion.with(this).load("http://zxm.buildface.com/api.php/qibo/region/allcities")
                .setBodyParameter("province_id", provinceid)
                .as(new TypeToken<BFNewModel<List<City>>>() {
                }).setCallback(new FutureCallback<BFNewModel<List<City>>>() {
            @Override
            public void onCompleted(Exception e, BFNewModel<List<City>> result) {
                if (null == e) {
                    if (result.getStatus().equals("success")) {
                        cities = result.getLists();
                        adapter = new QuickAdapter<City>(context, android.R.layout.simple_list_item_1) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, City item) {
                                helper.setText(android.R.id.text1, item.getCity());
                            }
                        };
                        listView.setAdapter(adapter);
                        adapter.addAll(cities);
                    } else
                        Toast.makeText(context, result.getStatus(), Toast.LENGTH_SHORT).show();
                } else {
                    e.printStackTrace();
                    Toast.makeText(context, "数据请求失败", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
        lisenter = (OnCitySelectedLisenter) activity;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != lisenter) {
            lisenter.citySelected(cities.get(position));
        }
        dismiss();
    }

    public interface OnCitySelectedLisenter {
        public void citySelected(City city);
    }
}
